#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>

int main(int argc, char *argv[])
{
	if(argc != 3){
		printf("input param error\n");
		return -1;
	}
	
	int s_fd = 0;
	int d_fd = 0;
	int i;
	int rd;
	int wr;
	printf("argc = %d\n",argc);
	//遍历argc 获得argv[]的参数
	for(i = 0;i < argc; i++)
	{
		printf("argv[%d] = %s\n",i,argv[i]);	
	}

	//打开原文件
	
	s_fd = open(argv[1],O_RDONLY);
	if(s_fd < 0)
	{
		perror("open file failed");
		return -1;
	}
	
	d_fd = open(argv[2],O_WRONLY | O_CREAT,S_IRWXU);
	if(d_fd < 0)
	{
		perror("open file failed");
		return -1;
	}
	
	char buf[1024];
	while(true) {
	    rd = read(s_fd, buf, sizeof(buf));
	    if(rd < 0){
		   perror("read file failed");
		   return -1;
	    }else if (rd == 0){
		   printf("copy %s success\n", argv[1]);
		   break;
	    }else if (rd > 1){
		   wr = write(d_fd, buf, rd);
		   if(wr < 0){
		      perror("write file failed");
		      return -1;
	       }
        }else{
		   printf("unknow error\n");
           return -1; 
	    }
    }
		
	return 0;
}